home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / BLENDE~1.ZIP / blender-2.37a-OSX-10.2-powerpc / plugins / texture / tiles.c < prev   
Encoding:
C/C++ Source or Header  |  2005-06-15  |  4.1 KB  |  177 lines

  1.  /**
  2.  * $Id: tiles.c,v 1.1 2003/01/01 15:06:08 hos Exp $
  3.  *
  4.  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License
  8.  * as published by the Free Software Foundation; either version 2
  9.  * of the License, or (at your option) any later version. The Blender
  10.  * Foundation also sells licenses for use in proprietary software under
  11.  * the Blender License.  See http://www.blender.org/BL/ for information
  12.  * about this.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software Foundation,
  21.  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  *
  23.  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  24.  * All rights reserved.
  25.  *
  26.  * The Original Code is: all of this file.
  27.  *
  28.  * Contributor(s): none yet.
  29.  *
  30.  * ***** END GPL/BL DUAL LICENSE BLOCK *****
  31.  */
  32.  
  33. #include "math.h"
  34. #include "plugin.h"
  35.  
  36. /* ******************** GLOBAL VARIABLES ***************** */
  37.  
  38. char name[]= "tiles";
  39.  
  40. /* Subtype names must be less than 15 characters */
  41.  
  42. #define NR_TYPES    2
  43. char stnames[NR_TYPES][16]= {"Square", "Deformed"};
  44.  
  45. VarStruct varstr[]= {
  46.      NUM|FLO,    "size",            1.0,     0.0, 1.0,  "The size of each tile", 
  47.      NUM|FLO,    "Noise",        1.0,     0.01, 10.0, ""
  48. };
  49.  
  50. /* The cast struct is for input in the main doit function
  51.    Varstr and Cast must have the same variables in the same order */ 
  52.  
  53. typedef struct Cast {
  54.     float size;
  55.     float noise;
  56. } Cast;
  57.  
  58. /* result: 
  59.    Intensity, R, G, B, Alpha, nor.x, nor.y, nor.z
  60.  */
  61.  
  62. float result[8];
  63.  
  64. /* cfra: the current frame */
  65.  
  66. float cfra;
  67.  
  68. int plugin_tex_doit(int, Cast *, float *, float *, float *);
  69.  
  70.  
  71. /* ******************** Fixed functions ***************** */
  72.  
  73. int plugin_tex_getversion(void) 
  74. {    
  75.     return B_PLUGIN_VERSION;
  76. }
  77.  
  78. void plugin_but_changed(int but) 
  79. {
  80. }
  81.  
  82. void plugin_init(void)
  83. {
  84. }
  85.  
  86. /* this function should not be changed: */
  87.  
  88. void plugin_getinfo(PluginInfo *info)
  89. {
  90.     info->name= name;
  91.     info->stypes= NR_TYPES;
  92.     info->nvars= sizeof(varstr)/sizeof(VarStruct);
  93.     
  94.     info->snames= stnames[0];
  95.     info->result= result;
  96.     info->cfra= &cfra;
  97.     info->varstr= varstr;
  98.  
  99.     info->init= plugin_init;
  100.     info->tex_doit=  (TexDoit) plugin_tex_doit;
  101.     info->callback= plugin_but_changed;
  102. }
  103.  
  104. /* ************************************************************
  105.     Tiles
  106.     
  107.     Demonstration of a simple square wave function sampled
  108.     with anti-aliasing.
  109.     It is not mipmapped yet...
  110.     
  111.    ************************************************************ */
  112.  
  113.  
  114. /* square wave, antialiased, no mipmap! */
  115.  
  116. float sample_wave(float freq, float coord, float pixsize)
  117. {
  118.     float fac, frac,  retval;
  119.     int part1, part2;
  120.     
  121.     if(pixsize > freq) return 0.5;
  122.     
  123.     pixsize/= freq;
  124.     
  125.     fac= coord/freq;
  126.     part1= ffloor(fac);
  127.     frac= fac - part1;
  128.  
  129.     if(part1 & 1) retval= 0.0;
  130.     else retval= 1.0;
  131.     
  132.     if(pixsize != 0.0) {
  133.         
  134.         /* is coord+pixsize another value? */
  135.         
  136.         part2= ffloor(fac + pixsize);
  137.         if(part1==part2) return retval;
  138.         
  139.         /* antialias */    
  140.         if(retval==1.0) retval= (1.0-frac)/pixsize;
  141.         else retval= 1.0-(1.0-frac)/pixsize;
  142.     }
  143.     return retval;
  144. }
  145.  
  146. /* return 0: One channel texture
  147.    return 1: RGB texture
  148.    return 2: Normals texture */
  149.  
  150.  
  151. int plugin_tex_doit(int stype, Cast *cast, float *texvec, float *dxt, float *dyt)
  152. {
  153.     float xwave, ywave;
  154.     
  155.     if(stype==1) {
  156.         texvec[0]+= hnoise(cast->noise, texvec[0], texvec[1], texvec[2]);
  157.         texvec[1]+= hnoise(cast->noise, texvec[1], texvec[2], texvec[0]);
  158.     }
  159.     
  160.     if(dxt && dyt) {
  161.         xwave= sample_wave(cast->size, texvec[0], fabs(dxt[0]) + fabs(dyt[0]) );
  162.         ywave= sample_wave(cast->size, texvec[1], fabs(dxt[1]) + fabs(dyt[1]) );
  163.  
  164.         if(xwave > ywave) result[0]= xwave-ywave;
  165.         else result[0]= ywave-xwave;
  166.     } 
  167.     else {
  168.         xwave= sample_wave(cast->size, texvec[0], 0.0 );
  169.         ywave= sample_wave(cast->size, texvec[1], 0.0 );
  170.         
  171.         if(xwave > ywave) result[0]= xwave-ywave;
  172.         else result[0]= ywave-xwave;
  173.     }
  174.  
  175.     return 0;
  176. }
  177.